
$(function () {
    $("#submits").click(function () {
        //实例化对象
        var TrainInfo = {};

        TrainInfo.Id = $("#userid").val();
        var id = TrainInfo.Id;
        TrainInfo.TrainTitle = $("#name").val();
        TrainInfo.speaker = $("#user").val();
        TrainInfo.StartTime = $("#demo").val()
        TrainInfo.EndTime = $("#demo01").val();
        TrainInfo.UserId = $("#person").val();
        TrainInfo.Address = $("#addr").val();


        //判断Id是否为空，true则进行添加操作
        if ($("#userid").val() == 0) {
            //温馨提示：是否添加用户
            layer.confirm('是否添加用户', function (index) {
                //ajax提交到后台进行添加用户操作
                $.ajax({
                    type: "post",
                    url: "/Login/Add",
                    data: TrainInfo,
                    success: function (operateResult) {
                        if (operateResult.Success) {
                            // 温馨提示：添加成功
                            //layer.msg('添加成功！', {
                            //    icon: 1,
                            //    time: 1000
                            //      }, function () {
                            //location.reload();//刷新页面
                            //layer.close(index);
                            //});
                            window.location.href = "/Home/Train";
                        } else {
                            //温馨提示：添加失败
                            layer.msg('添加失败！', {
                                icon: 1,
                                time: 1000
                            });
                        }
                    },
                })
            })
            //如果Id是不为空，false则进行修改操作
        } else {
            $.ajax({
                type: "post",
                url: "/Login/Update",
                data: TrainInfo,
                success: function (operateResult) {
                    //温馨提示：修改成功
                    if (operateResult.Success) {
                        layer.msg('修改成功！', {
                            icon: 1,
                            time: 1000
                        }, function () {
                            location.reload();//刷新页面
                            layer.close(index);
                        });
                    } else {
                        //温馨提示：添加失败
                        layer.msg('修改失败！', {
                            icon: 1,
                            time: 1000
                        });
                    }
                }
            })
            //温馨提示：是否修改用户
            //layer.confirm('是否修改用户', function (index) {
            //    //ajax提交到后台进行修改用户操作

            //})
        }

    });
})

$.validator.setDefaults({
    submitHandler: function () {
        alert("修改成功");
    }
});
$().ready(function () {
    $("#form_demo").validate();
});


function change() {
    var height01 = $(window).height();
    $(".top").css('height', height01 - 35 + "px");
}
var PROJECTID, TASKTYPE, TASKPHASE, PERSONID
!function () {
    laydate.skin('danlan');//切换皮肤，请查看skins下面皮肤库
    laydate({ elem: '#demo' });//绑定元素
    laydate({ elem: '#demo1' });
    laydate({ elem: '#demo2' });//绑定元素
}();
function init() {
    if (parent.getCurrentID() != "") {
        $.ajax({
            url: '../TaskRecord/SearchTaskById?taskId=' + parent.getCurrentID(),
            type: 'POST',
            dataType: 'json',
            success: function (result) {
                var data = result.data;
                if (data) {
                    PROJECTID = data.PROJECTID
                    PERSONID = data.PERSONID
                    TASKPHASE = data.TASKPHASE;
                    TASKTYPE = data.TASKTYPE;
                    $("#name").val(data.PROJECTNAME);
                    $("#user").val(data.TASKPHASENAME);
                    $("#addr").val(data.STATE);

                    $("#demo").val(getFormatTime(data.STARTTIME.substring(6, 19)));
                    $("#demo01").val(getFormatTime(data.STARTTIME.substring(6, 19)));
                    $("#person").val(data.NOTE);
                    $("#core").val(data.NOTE);
                    $("#editor").val(data.NOTE);



                }
                else {
                    alert("获取失败！");
                }
            },
            error: function (err) {
            }
        })


    }
    function Save() {
        $.ajax({
            type: "POST",
            url: "../TaskRecord/UpdateTask",
            data: {
                TASKID: parent.getCurrentID(),
                PROJECTID: PROJECTID,
                STATE: $("#name").val(),
                TASKPHASE: $("#user").val(),
                TASKPHASE: $("#addr").val(),
                STARTTIME: $("#demo").val(),
                STARTTIME: $("#demo01").val(),
                TASKTYPE: $("#person").val(),
                TASKTYPE: $("#core").val(),
                NOTE: $("#editor").val(),



            },
            dataType: "~/json",
            success: function (result) {
                if (result.data) {
                    alert("保存成功！！！");
                    parent.getNoticeTableData();
                    TaskCancel();
                } else {
                    alert("保存失败！！！")
                }
            }
        })
    }
    function TaskCancel() {
        var index = parent.layer.getFrameIndex(window.name)
        parent.layer.close(index);
    }
    //初始化部门下拉菜单
    //    function getPHASE() {
    //        $.ajax({
    //            url: '../Common/GetPhaseList',
    //            type: 'GET',
    //            dataType: 'json',
    //            success: function (data) {
    //                var PHASEValue = data.data;
    //                if (PHASEValue.length > 0) {
    //                    $("#part").html("");
    //                    for (var i = 0; i < PHASEValue.length; i++) {
    //                        if (TASKPHASE == PHASEValue[i].ID) {
    //                            var html = "<Option value = '" + PHASEValue[i].ID + "'  selected = 'true'>" + PHASEValue[i].NAME + "</Option>";
    //                        } else {
    //                            var html = "<Option value = '" + PHASEValue[i].ID + "'>" + PHASEValue[i].NAME + "</Option>";
    //                        };
    //                        $("#part").append(html);
    //                    }
    //                }
    //
    //
    //            },
    //            error: function (err) {
    //            }
    //
    //        })
    //    }


    //时间格式化函数
    function getFormatTime(time) {
        var time = new Date(parseInt(time));
        var y = time.getFullYear();
        var m = time.getMonth() + 1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    }
    function add0(m) { return m < 10 ? '0' + m : m }

